<script type="text/javascript">
$(function() {
	var pg = App.wechat.msgTypeQry.ui , pp = pg.params
	, gd = pg.seek('grid');
	gd.mac('grid', {
		key: 'id',
		cols: [{
			field: 'msgCode', title : '消息编号', width: 150, sort: true,
		}, {
			field: 'wechatTemplate', title : '微信模板', width: 300, sort: true,
			render: function(r) {
				return r.wechatTemplate.name;
			}
		}, {
			field: 'status', title : '发布状态', width: 100, sort: true,
			render: function(r) {
				if (r.status == '0') {
					return '未发布';
				} else {
					return '已发布';
				}
			}
		}, {
			field: 'memo', title : '备注', width: 300, sort: true,
		}, {
			field: 'id', title : '操作', width: 150,
			render: function(r) {
				var p = $('<div></div>');
						
				$('<a style="padding: 15px;">编辑</a>').click(function () {
					App.open({
						title : '消息类型编辑',
						ctx : 'wechat',
						action : 'msgTypeEdit',
						params : r
					}, function () {
						gd.load();
					});
				}).appendTo(p);
					
				$('<a>删除</a>').click(function () {
					App.open({
						title : '删除确认',
						ctx : 'wechat',
						action : 'msgTypeDelConfirm',
						params : r
					}, function () {
						$.post(App.ctx + "/msg/delete.do", r, function (rs) {
							var ro = mac.eval(rs);
							if (ro.success) {
								App.open({
									title : '删除结果',
									ctx : 'wechat',
									action : 'msgTypeDelSuccess',
									params : {}
								}, function() {
									gd.load();
								});
							} else {
								App.open({
									title : '删除结果',
									ctx : 'wechat',
									action : 'msgTypeDelFailed',
									params : ro.data
								});
							}
						});
					});
				}).appendTo(p);
				
				// 未发布状态显示发布按钮，否则显示卸载按钮
				if (r.status == 0) {
					$('<a style="padding: 15px;">发布</a>').click(function () {
						App.open({
							title : '消息类型发布',
							ctx : 'wechat',
							action : 'msgTypePublishConfirm',
							params : r
						}, function () {
							$.post(App.ctx + "/msg/publish.do", r, function (rs) {
								var ro = mac.eval(rs);
								if (ro.success) {
									App.open({
										title : '删除结果',
										ctx : 'wechat',
										action : 'msgTypePublishSuccess',
										params : {}
									}, function() {
										gd.load();
									});
								} else {
									App.open({
										title : '删除结果',
										ctx : 'wechat',
										action : 'msgTypePublishFailed',
										params : ro.data
									});
								}
							});
						});
					}).appendTo(p);
				} else {
					$('<a style="padding: 15px;">卸载</a>').click(function () {
						App.open({
							title : '消息类型卸载',
							ctx : 'wechat',
							action : 'msgTypeUninstallConfirm',
							params : r
						}, function () {
							$.post(App.ctx + "/msg/uninstall.do", r, function (rs) {
								var ro = mac.eval(rs);
								if (ro.success) {
									App.open({
										title : '卸载结果',
										ctx : 'wechat',
										action : 'msgTypeUninstallSuccess',
										params : {}
									}, function() {
										gd.load();
									});
								} else {
									App.open({
										title : '卸载结果',
										ctx : 'wechat',
										action : 'msgTypeUninstallFailed',
										params : ro.data
									});
								}
							});
						});
					}).appendTo(p);
				}
				
				return p;
			}
		}],
		loader: {
			url: App.ctx + '/msg/list.do',
			params: {
				pageNo: 1, pageSize: 10,
				orderBy: 'msgCode asc'
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	
	function adjust() {
		var de = document.documentElement, 
		ch = de.clientHeight;
		
		pg.height(ch);
		gd.parent().height(ch - 100);
		gd.adjust();
	}
	
	$(window).resize(adjust);
	adjust();
	
	pg.seek('search').click(function (e) {
		gd.load({
			templateCode : pg.seek('qTemplate').val()
		});
	});
	pg.seek('add').click(function () {
		App.open({
			title : '消息类型编辑',
			ctx : 'wechat',
			action : 'msgTypeEdit'
		}, function () {
			gd.load();
		});
	});
});
</script>
<div>
	<form role="form">
		<div class="item">
			<span class="label">模板编号</span>
			<input type="text" class="form-control" name="qTemplate" placeholder="微信模板编号">
			<button name="search" type="button" class="btn radius btn-white btn-sm">查询</button>
			<button name="add" type="button" class="btn radius btn-info btn-sm">新增</button>
		</div>
	</form>
</div>
<div class="body">
	<div class="row">
		<div class="col-xs-12">
			<div name="grid" class="grid"></div>
		</div>
	</div>
</div>
